﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Net;
using System.Xml.Linq;
using System.Reflection;

namespace SharePoint.Utilities.Cmdlets
{
    public static class ProxyHelper
    {

        private static BasicHttpBinding GetBindingForChannel()
        {
            BasicHttpBinding binding = new BasicHttpBinding();
            binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
            binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Ntlm;
            binding.MaxBufferSize = 10065536;
            binding.MaxReceivedMessageSize = 10065536;
            binding.ReaderQuotas.MaxStringContentLength = 8192000;
            return binding;
        }

        public static WebsSoap WebsChannelFactory(string SiteCollectionUrl)
        {

            EndpointAddress address = new EndpointAddress(SiteCollectionUrl + "/_vti_bin/webs.asmx");
            ChannelFactory<WebsSoap> channel = new ChannelFactory<WebsSoap>(GetBindingForChannel(), address);
            channel.Credentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
            channel.Credentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;
            WebsSoap wcfclient = channel.CreateChannel();
            return wcfclient;
        }


        public static TaxonomywebserviceSoap TaxonomyChannelFactory(string SiteCollectionUrl)
        {
            EndpointAddress address = new EndpointAddress(SiteCollectionUrl + "/_vti_bin/TaxonomyClientService.asmx");
            ChannelFactory<TaxonomywebserviceSoap> channel = new ChannelFactory<TaxonomywebserviceSoap>(GetBindingForChannel(), address);
            channel.Credentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
            channel.Credentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;
            TaxonomywebserviceSoap wcfclient = channel.CreateChannel();
            return wcfclient;
        }
        
    }
}
